package com.lei.zjs.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 流程定义Action
 * @author Spinacia
 *
 */
@Controller
@Scope("prototype")
public class ProcessDefinitionAction extends ActionSupport{
	@Resource
	private ProcessEngine processEngine;
	//查询所有最新的流程定义
	public String list(){
		ProcessDefinitionQuery definitionQuery = processEngine.getRepositoryService().createProcessDefinitionQuery();
		definitionQuery.latestVersion();
		List<ProcessDefinition> list = definitionQuery.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}
	
	
	private File deploy;
	
	public void setDeploy(File deploy) {
		this.deploy = deploy;
	}

	//部署流程
	public String deploy() throws FileNotFoundException{
		DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
		InputStream in = new FileInputStream(deploy);
		deploymentBuilder.addZipInputStream(new ZipInputStream(in));
		deploymentBuilder.deploy();
		return "tolist";
	}
	
	
	private String id;
	
	public void setId(String id) {
		this.id = id;
	}

	private InputStream pngStream;
	
	public InputStream getPngStream() {
		return pngStream;
	}

	//查看流程图
	public String viewpng(){
		ProcessDefinitionQuery definitionQuery = processEngine.getRepositoryService().createProcessDefinitionQuery();
		definitionQuery.processDefinitionId(id);
		ProcessDefinition processDefinition = definitionQuery.singleResult();
		String deploymentId = processDefinition.getDeploymentId();
		String pngName = processDefinition.getDiagramResourceName();
		pngStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId, pngName);
		return "viewpng";
	}
}
